package com.qiguliuxing.dts.db.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qiguliuxing.dts.db.domain.*;
import com.qiguliuxing.dts.db.util.OrderUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.bean.DayStatis;
import com.qiguliuxing.dts.db.dao.DtsUserAccountMapper;
import com.qiguliuxing.dts.db.dao.DtsUserMapper;
import com.qiguliuxing.dts.db.dao.ex.StatMapper;

@Service
public class DtsUserService {

	@Resource
	private DtsUserMapper userMapper;

	@Resource
	private DtsUserAccountMapper userAccountMapper;

	@Resource
	private StatMapper statMapper;
	@Resource
	private DtsAdminService adminService;

	public DtsUser findById(Integer userId) {
		return userMapper.selectByPrimaryKey(userId);
	}

	public UserVo findUserVoById(Integer userId) {
		DtsUser user = findById(userId);
		UserVo userVo = new UserVo();
		userVo.setNickname(user.getNickname());
		userVo.setAvatar(user.getAvatar());
		if(!ObjectUtils.isEmpty(user.getDoctorUserId())){
			DtsAdmin dtsAdmin = adminService.findById(user.getDoctorUserId());
			if(!ObjectUtils.isEmpty(dtsAdmin)){
				userVo.setDoctorName(dtsAdmin.getNickName());
			}
		}
		return userVo;
	}

	public DtsUser queryByOid(String openId) {
		DtsUserExample example = new DtsUserExample();
		example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
		return userMapper.selectOneByExample(example);
	}

	public void add(DtsUser user) {
		user.setAddTime(LocalDateTime.now());
		user.setUpdateTime(LocalDateTime.now());
		userMapper.insertSelective(user);
	}

	public int updateById(DtsUser user) {
		user.setUpdateTime(LocalDateTime.now());
		return userMapper.updateByPrimaryKeySelective(user);
	}

	public List<DtsUser> querySelective(String username, String mobile, Integer page, Integer size, String sort,
			String order) {
		DtsUserExample example = new DtsUserExample();
		DtsUserExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(username)) {
			criteria.andNicknameLike("%" + username + "%");
		}
		if (!StringUtils.isEmpty(mobile)) {
			criteria.andMobileEqualTo(mobile);
		}
		criteria.andDeletedEqualTo(false);

		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}

		PageHelper.startPage(page, size);
		return userMapper.selectByExample(example);
	}

	public int count() {
		DtsUserExample example = new DtsUserExample();
		example.or().andDeletedEqualTo(false);

		return (int) userMapper.countByExample(example);
	}

	public List<DtsUser> queryByUsername(String username) {
		DtsUserExample example = new DtsUserExample();
		example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
		return userMapper.selectByExample(example);
	}

	public List<DtsUser> queryByDoctorId(DtsUser dtsUser,Integer page, Integer size, String sort, String order) {
		DtsUserExample example = new DtsUserExample();
		DtsUserExample.Criteria criteria = example.createCriteria();
		criteria.andDoctorIdEqualTo(dtsUser.getDoctorUserId()).andAliasDeletedEqualTo(false);
		if (!StringUtils.isEmpty(dtsUser.getBindDoctorStartTime()) && !StringUtils.isEmpty(dtsUser.getBindDoctorEndTime())) {
			criteria.andBindDoctorTimeLessThan(dtsUser.getBindDoctorStartTime()).andBindDoctorTimeGreaterThan(dtsUser.getBindDoctorEndTime());
		}

		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}

		PageHelper.startPage(page, size);
		return userMapper.selectByDoctorId(example);
	}

	public boolean checkByUsername(String username) {
		DtsUserExample example = new DtsUserExample();
		example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
		return userMapper.countByExample(example) != 0;
	}

	public List<DtsUser> queryByMobile(String mobile) {
		DtsUserExample example = new DtsUserExample();
		example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
		return userMapper.selectByExample(example);
	}

	public List<DtsUser> queryByOpenid(String openid) {
		DtsUserExample example = new DtsUserExample();
		example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
		return userMapper.selectByExample(example);
	}

	public void deleteById(Integer id) {
		userMapper.logicalDeleteByPrimaryKey(id);
	}

	/**
	 * 审批代理申请
	 * @param
	 */
	public void approveAgency(Integer userId,Integer settlementRate,String shareUrl) {
		//获取账户数据
		DtsUserAccountExample example = new DtsUserAccountExample();
		example.or().andUserIdEqualTo(userId);

		DtsUserAccount dbAccount = userAccountMapper.selectOneByExample(example);
		if (dbAccount == null) {
			throw new RuntimeException("申请账户不存在");
		}
		dbAccount.setShareUrl(shareUrl);
		if (!StringUtils.isEmpty(settlementRate)) {
			dbAccount.setSettlementRate(settlementRate);
		}
		dbAccount.setModifyTime(LocalDateTime.now());
		userAccountMapper.updateByPrimaryKey(dbAccount);

		//更新会员状态和类型
		DtsUser user = findById(userId);
		user.setUserLevel((byte) 2);//区域代理用户
		user.setStatus((byte) 0);//正常状态
		updateById(user);

	}

	public DtsUserAccount detailApproveByUserId(Integer userId) {
		// 获取账户数据
		DtsUserAccountExample example = new DtsUserAccountExample();
		example.or().andUserIdEqualTo(userId);

		DtsUserAccount dbAccount = userAccountMapper.selectOneByExample(example);
		return dbAccount;
	}

	public List<DtsUser> queryDtsUserListByNickname(String username,String mobile) {
		DtsUserExample example = new DtsUserExample();
		DtsUserExample.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(username)) {
			criteria.andNicknameLike("%" + username + "%");
		}
		if (!StringUtils.isEmpty(mobile)) {
			criteria.andMobileEqualTo(mobile);
		}
		criteria.andDeletedEqualTo(false);
		return userMapper.selectByExample(example);
	}

	public List<DayStatis> recentCount(int statisDaysRang) {
		return statMapper.statisIncreaseUserCnt(statisDaysRang);
	}

	/**
	 * 用户购买记录
	 * @return
	 */
	public List<Map<String,Object>> memberBuyChat(DtsUser dtsUser,Integer page, Integer size, String sort, String order) {
		DtsUserExample example = new DtsUserExample();
		DtsUserExample.Criteria criteria = example.createCriteria();
		criteria.andDoctorIdEqualTo(dtsUser.getDoctorUserId()).andOrderStatusGreaterThan(OrderUtil.STATUS_PAY + "");
		if (!StringUtils.isEmpty(dtsUser.getBuyStartTime()) && !StringUtils.isEmpty(dtsUser.getBuyEndTime())) {
			criteria.andBuyTimeLessThan(dtsUser.getBuyStartTime()).andBuyTimeGreaterThan(dtsUser.getBuyEndTime());
		}
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}

		PageHelper.startPage(page, size);
		return userMapper.memberBuyChat(example);
	}

	/**
	 * 用户消费金额
	 * @return
	 */
	public List<Map<String,Object>> memberConsumeAmount(DtsUser dtsUser,Integer page, Integer size, String sort, String order) {
		DtsUserExample example = new DtsUserExample();
		DtsUserExample.Criteria criteria = example.createCriteria();
		criteria.andDoctorIdEqualTo(dtsUser.getDoctorUserId()).andOrderStatusGreaterThan(OrderUtil.STATUS_PAY + "");
		if (!StringUtils.isEmpty(dtsUser.getBuyStartTime()) && !StringUtils.isEmpty(dtsUser.getBuyEndTime())) {
			criteria.andBuyTimeLessThan(dtsUser.getBuyStartTime()).andBuyTimeGreaterThan(dtsUser.getBuyEndTime());
		}

		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}

		PageHelper.startPage(page, size);
		return userMapper.memberConsumeAmount(example);
	}

	/**
	 * 用户复购率
	 * @return
	 */
	public Map<String,Object> memberRepurchaseRate() {
		return userMapper.memberRepurchaseRate();
	}


	/**
	 * 医生销售统计
	 * @return
	 */
	public List<Map<String,Object>> doctorAmount(DtsUser dtsUser,Integer page, Integer size, String sort, String order) {
		DtsUserExample example = new DtsUserExample();
		DtsUserExample.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(dtsUser.getPayMonth())) {
			criteria.andPayTimeMonth(dtsUser.getPayMonth());
		}
		PageHelper.startPage(page, size);
		return userMapper.doctorAmount(example);
	}
}
